<?php
namespace Home\Controller;
use Think\Controller;
class GoodsController extends CommonController {
     
     //浏览商品信息
    public function index(){
       
       //实例化商品信息操作对象 
       $mod = M("goods");
       // <!--array(array(id=>成人,id=>商务，id=>时尚),id=>时尚)-->
       //查询分类
       $menu = M('cate');
        $cate = $menu->field('id,name,level')->select();

        foreach($cate as $k=>$v)
        {
            $arr[$v['level']] = array();

        }
        
        foreach($cate as $k=>$v)
        {
            $arr[$v['level']][] = $v;
        }
        $this->assign('cate1',$arr[1]);    
        $this->assign('cate2',$arr[2]);    
        $this->assign('title','产品中心');

        $cid = I('get.cid/d');
        $cate_lists = array();
       if($cid)
       {
          $map['cid'] =  array('EQ',$cid);
          $cateinfo = M("cate")->field('name,pid,keywords,description')->where("id=".$cid)->find();
          if($cateinfo['pid']!=0){
            $cate_lists = M("cate")->where("pid=".$cateinfo['pid'])->select();
          }
          $this->assign('keywords',$cateinfo['keywords']);
          $this->assign('description',$cateinfo['description']);
          $this->assign('title',$cateinfo['name'].'　｜　产品中心');
       }

       $count = $mod->where($map)->count();// 查询满足要求的总记录数
       
       $Page = new \Think\PageHome($count,8);// 实例化分页类 传入总记录数和每页显示的记录数(25)

       $Page -> setConfig('first','<<');
       $Page -> setConfig('prev','<');
       $Page -> setConfig('next','>');
       $Page -> setConfig('link','indexpagenumb');//pagenumb 会替换成页码
       $Page -> setConfig('theme',' %FIRST% %UP_PAGE% %NOW_PAGE%/%TOTAL_PAGE% 页 %DOWN_PAGE% %END%');
       
       $show = $Page->show();// 分页显示输出
       // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
       $goods=$mod->join('cate ON goods.cid=cate.id','LEFT')->field("goods.id,goods.pic,goods.gname,goods.description,cate.name as cname")->order('goods.addtime desc')->limit($Page->firstRow.','.$Page->listRows)->where($map)->select();

       //放置模板中

       $this->assign("goods",$goods);
       $this->assign("cate_lists",$cate_lists);
       $this->assign('page',$show);// 赋值分页输出
       
       //加载模板输出
       $this->display("index");
    }

    //分类AJAX请求
    public function cate()
    {
      $pid = I('get.pid/d');

      $mod = M('cate');
      if($pid)
      {
        $children = $mod->field('id,name')->where('pid='.$pid)->select();
        if($children)
        {
          $str = '';
          foreach($children as $k=>$v)
          {
            
            $str .= "<li pid=".$v['id']."><a href='".U('Goods/index?cid='.$v['id'])."'>".$v['name']."</a></li>";
          }
          $data['status'] = 1;
          $data['str'] = $str;
          $this->ajaxReturn($data);
          
        }
        else
        {
          $url = U('goods/index?cid='.$pid);
          $data['status'] = 0;
          $data['url'] = $url;
          $this->ajaxReturn($data);
          
        }
      }

    }

    //详情页
    public function detail()
    {
       $id = I('get.id/d');
       $detail = M('goods')->find($id);
       $detail['direction'] = htmlspecialchars_decode($detail['direction']);
       // dump($detail);die;
       $this->assign('keywords',$detail['keywords']);
       $this->assign('description',$detail['description']);
       $this->assign('detail',$detail);
       $this->assign('title',$detail['gname'].'　｜　产品中心');
       $this->display('detail');
    }
}

class Tool {
    static public $treeList = array(); //存放无限分类结果如果一页面有多个无限分类可以使用 Tool::$treeList = array(); 清空
    /**
     * 无限级分类
     * @access public 
     * @param Array $data     //数据库里获取的结果集 
     * @param Int $pid             
     * @param Int $count       //第几级分类
     * @return Array $treeList   
     */
    static public function tree(&$data,$pid = 0,$count = 1) {
        foreach ($data as $key => $value){
            if($value['pid']==$pid){
                $value['level'] = $count;
                self::$treeList []=$value;
                unset($data[$key]);
                self::tree($data,$value['id'],$count+1);
            } 
        }
        return self::$treeList ;
    }
    
}